package chapter_13_proxy.dynamic_proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;

/**
 * Created by CharloYao on 2017/10/11.
 * 动态代理类对应的调用处理程序类
 */
public class SubjectInvocationHandler implements InvocationHandler{

    private Object delegate;

    public SubjectInvocationHandler(Object delegate) {
        this.delegate = delegate;
    }

    @Override
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        long stime = System.currentTimeMillis();

        /**
         *  利用反射机制将请求分派给委托类处理
         *  method.invoke()返回object结果，
         *  因为实例程序没有返回值，所以直接返回了null
         */
        Object object = method.invoke(delegate, args);

        long etime = System.currentTimeMillis();
        System.out.println("执行时间：" + (etime - stime) + "毫秒");
        return object;
    }
}
